<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\OnlineExhibitionTable;
use App\Admin\Repositories\OnlineExhibitionEvaluation;
use App\Models\OnlineExhibition;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class OnlineExhibitionEvaluationController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new OnlineExhibitionEvaluation(['onlineExhibition']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('onlineExhibition.name',"展厅");
            $grid->column('created_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal("onlineExhibition.id", "展厅")
                    ->selectTable(OnlineExhibitionTable::make())
                    ->title("展厅")
                    ->model(OnlineExhibition::class, 'id', "name");
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OnlineExhibitionEvaluation(['onlineExhibition']), function (Show $show) {
            $show->field('id');
            $show->field('online_exhibition.name',"展厅");
            $show->field('content')->unescape();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new OnlineExhibitionEvaluation(), function (Form $form) {
            $form->display('id');
            $form->selectTable("online_exhibition_id")
                ->title('展厅名')
                ->dialogWidth('50%') // 弹窗宽度，默认 800px
                ->from(OnlineExhibitionTable::make(['id' => $form->getKey()])) // 设置渲染类实例，并传递自定义参数
                ->model(OnlineExhibition::class, 'id', 'name')
                ->rules("unique:online_exhibition_evaluations,online_exhibition_id,".$form->model()->id)->required(); // 设置编辑数据显示
            $form->editor('content')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
